#include "Printer.h"

#include <iostream>

static const char* TOTAL_COUNT_SYMBOL = "$";
static const char* SKIPPED_PAIR_SYMBOL = "*";
static const char* STEP_SEPARATOR_SYMBOL = "!";
static const char* SYNC_SYMBOL = "S";

Printer::Printer(bool tsv_enabled) : tsv_enabled_(tsv_enabled) { }

bool Printer::printAndSync(const FilePairType& file_pair, AlgResultType sim) {

    if (sim.which() == 1 &&
        boost::get<OptionalResult>(sim) == UNKNOWN) {
        return true;
    }

    std::unique_lock<std::mutex> lock(mutex_);

    std::cout << SKIPPED_PAIR_SYMBOL << std::endl;

    auto print_sim = [=] (decltype(sim) arg, const std::string& delim) {
        if (arg.which() == 1) {
            std::cout << delim << "uncomparable" << std::endl;
        } else {
            if (boost::get<double>(arg) == 1) {
                std::cout << delim << "full match" << std::endl;
            } else {
                std::cout << delim << arg << std::endl;
            }
        }
    };

    if (tsv_enabled_) {
        std::cout << "\t" << file_pair.first->filename;
        std::cout << "\t" << file_pair.second->filename;
        print_sim(sim, "\t");
    } else {
        std::cout << file_pair.first->filename << "\n";
        std::cout << file_pair.second->filename << std::endl;
        print_sim(sim, "");
    }

    std::cout << SYNC_SYMBOL << std::endl;

    std::string line;
    std::cin >> line;

    lock.unlock();
    return true;
}

void Printer::startSession(size_t count) const {

    std::cout << TOTAL_COUNT_SYMBOL << count << std::endl;
}

void Printer::endSession() const {

    std::cout << STEP_SEPARATOR_SYMBOL << std::endl;
}
